package com.lzh.farmersshop.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzh.farmersshop.entity.Type;
import com.lzh.farmersshop.util.Result;
import lombok.extern.slf4j.Slf4j;
import com.lzh.farmersshop.entity.Banner;
import com.lzh.farmersshop.service.BannerService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * (Banner)表控制层
 *
 * @author makejava
 * @since 2024-01-04 15:41:50
 */
@Slf4j
@RestController
@RequestMapping("admin/banner")
@Transactional(rollbackFor = Exception.class)
public class BannerController {
    /**
     * 服务对象
     */
    @Resource
    private BannerService bannerService;

    /**
     * 分页查询所有数据
     *
     * @param banner 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public Result selectAll(Banner banner,Integer page,Integer limit) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        LambdaQueryWrapper<Banner> q = new LambdaQueryWrapper<>();
        q.like(StringUtils.hasLength(banner.getAltname()), Banner::getAltname, banner.getAltname());
        HashMap<String, Object> result = new HashMap<>();
        result.put("count", bannerService.list(q).size());
        //分页
        q.last("limit " + (page - 1) * limit + "," + limit);
        result.put("data", bannerService.list(q));
        return Result.success(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        return Result.success(bannerService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param banner 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public Result insert(Banner banner) {
        if (banner == null) {
            return Result.failure("数据不能为空");
        }
        banner.setIsdelete(1);

        if (bannerService.save(banner)){
            return Result.success("新增成功");
        }
        return Result.failure("新增失败");
    }

    /**
     * 修改数据
     *
     * @param banner 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Result update(Banner banner) {
        if (banner == null) {
            return Result.failure("数据不能为空");
        }
        if (bannerService.updateById(banner)){
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @PostMapping("delete")
    public Result delete(Integer id) {
        //判断id是否为空
        if (id == null) {
            return Result.failure("id不能为空");
        }

        Banner banner = new Banner();
        banner.setIsdelete(2);
        banner.setId(id);

        if (bannerService.removeById(banner)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }
}

